package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.TTasks;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.service.TTaskService;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;
import com.ldatum.crm.serviceImpl.TTaskServiceImpl;

@ParentPackage("admin")

public class TaskAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private TTasks task = new TTasks();
	
	List<TTasks> taskList = new ArrayList<TTasks>();
	List<TCustomer> customerList = new ArrayList<TCustomer>();
	List<TEmployee> employeeList = new ArrayList<TEmployee>();
	
	TTaskService taskService = new TTaskServiceImpl();
	TEmployeeService employeeService = new TEmployeeServiceImpl();
	
	private Integer id;
	private String theme;
	private String serveTime;
	private String customerName;
	private String serviceProject;
	private String status;
	private String alarmType;
	private String alarmTime;
	private String executeMen;
	private String instruction;
	private String createTime;
	
	private String levelOne;
	private String levelTwo;
	
	public void viewById(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			task = taskService.findById(id);
			json.accumulate("task", task);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String findById(){
		task = taskService.findById(id);
		try {
//			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "dailyManagement";
		levelTwo = "task";
		return "editTask";
	}
	
	public void save(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			task = getTaskFromPage();
			taskService.save(task);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	

	public void update(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			task = getTaskFromPage();
			taskService.update(task);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String list() throws Exception{
		String creater = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
		taskList = taskService.list(TTasks.class,creater);
		levelOne = "dailyManagement";
		levelTwo = "task";
		return "listTasks";
	}
//	@Test
	public String add() throws Exception{
		try {
//			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "dailyManagement";
		levelTwo = "task";
		return "addTask";
	}

	public String edit() throws Exception{
		try {
//			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			task = taskService.findById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "task";
		return "editTask";
	}
	
	public TTasks getTaskFromPage() {
		
		return task;
	}
	
	public TTasks getTask() {
		return task;
	}

	public void setTask(TTasks task) {
		this.task = task;
	}

	public List<TTasks> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<TTasks> taskList) {
		this.taskList = taskList;
	}

	public List<TCustomer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getServeTime() {
		return serveTime;
	}

	public void setServeTime(String serveTime) {
		this.serveTime = serveTime;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getServiceProject() {
		return serviceProject;
	}

	public void setServiceProject(String serviceProject) {
		this.serviceProject = serviceProject;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAlarmType() {
		return alarmType;
	}

	public void setAlarmType(String alarmType) {
		this.alarmType = alarmType;
	}

	public String getAlarmTime() {
		return alarmTime;
	}

	public void setAlarmTime(String alarmTime) {
		this.alarmTime = alarmTime;
	}

	public String getExecuteMen() {
		return executeMen;
	}

	public void setExecuteMen(String executeMen) {
		this.executeMen = executeMen;
	}

	public String getInstruction() {
		return instruction;
	}

	public void setInstruction(String instruction) {
		this.instruction = instruction;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
}
